home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 80 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.9 KB  |  107 lines

  1. Path: hubcap.clemson.edu!hubcap!mjs
  2. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: switching array pointers query
  5. Date: 1 Jan 96 16:51:09 GMT
  6. Organization: Clemson University
  7. Message-ID: <mjs.820515069@hubcap>
  8. References: <95123120361029516@tglbbs.com>
  9. NNTP-Posting-Host: hubcap.clemson.edu
  10.  
  11. charles.herold@tglbbs.com (Charles Herold) writes:
  12.  
  13. >My struggle with arrays of pointers and such not continues, and here's
  14. >an example of my difficulties.  Below is a bit of code that works, after
  15. >trying various ways that didn't work.  However, it doesn't do what I
  16. >want quite the way I intended.  I have an array of pointers to
  17. >structures, and wish to simply reverse the order of the array.  What I
  18. >*want* is to simply change what the pointers point to, that is, the
  19. >first pointer points to what begins as the last element of the array,
  20. >the last points to the first, and so on.  But I couldn't get that, and
  21. >you see below that I am simply moving the information in the last
  22. >element into where the first pointer is pointing etc.  Since it
  23. >does work, it might seem like I could just not worry about it.  But I'm
  24. >hoping seeing how this should be done might give me an understanding
  25. >breakthrough on this whole pointer to array thing that drives me crazy.
  26.  
  27. >Thanks to all who help.
  28.  
  29. >void
  30. >ReverseFiles( struct find_t **files, int numberfiles )
  31.  
  32. /* files is a pointer to the first element of an array of pointers
  33. to structs.  So to refer to the i'th pointer in this array, you will
  34. want to write "files[i]", and to refer to the struct it points to, you
  35. will write "*files[i]". */
  36.  
  37. >{
  38. >    int i, h;
  39. >    struct find_t temp;
  40.  
  41. /* since you want to swap struct pointers, your temp should be a 
  42. struct pointer
  43.  
  44.     struct find_t *temp;
  45. */
  46.  
  47. >    for( i = 0, h = numberfiles - 1; i < h; i++, h-- ) {
  48. >         temp = (*files)[i];
  49. >         (*files)[i] = (*files)[h];
  50. >         (*files)[h] = temp;
  51.  
  52. /* now, your swap should be
  53.  
  54.          temp = files[i];
  55.          files[i] = files[h];
  56.          files[h] = temp;
  57.  
  58. */
  59.  
  60. >    }
  61. >}
  62.  
  63. Let's take a moment to look at what you wrote, though, because it's 
  64. instructive to see why it appears to work.  (By "work" here, I mean
  65. reorder the structs themselves--as you have correctly reasoned or
  66. discovered that it does.)
  67.  
  68. The expression (*files)[i] is evaluated as follows:
  69.  
  70.     *files dereferences the pointer files, which is pointing
  71.     to the first element in the array of pointers.  Let's store 
  72.     this value in a temporary, say 
  73.  
  74.         struct find_t *x = *files;
  75.  
  76.     Now we want to know about x[i].  This is evaluated as
  77.     *(x + i): it's value is the value of the i'th struct
  78.     past the one pointed to by x, in an array of structs.
  79.  
  80. Now, x *happens* to be pointing to the first struct in your array of
  81. structs, so x[i] (hence (*files)[i]) *happens* to refer to the i'th
  82. struct, and the code succeeds in physically reversing the elements of
  83. the array of structs.  But if the first pointer in the list *happened*
  84. to point to a different element (or if the structs were not stored in
  85. an array at all, but were malloc'ed independently), this wouldn't work
  86. at all: at least some of the array references would be outside the
  87. legal range.  Furthermore, if *any* of the pointers pointed out of
  88. sequence, this would probably not do what you expected.  Compare it to
  89.  
  90.          temp = *files[i];
  91.          *files[i] = *files[h];
  92.          *files[h] = temp;
  93.  
  94. which swaps the struct pointed to by the i'th and h'th pointers.  So
  95. this version moves the struct pointed to by files[0] to the location
  96. pointed to by files[numberfiles - 1], etc., no matter in what sequence
  97. the pointers referred to the structs (or even if the structs weren't
  98. stored in an array).
  99.  
  100. Try tracing through the results of running each of these versions
  101. twice on a short array (reversing and reversing again), to see how
  102. the three versions behave differently.
  103. -- 
  104.         Matthew Saltzman
  105.         Clemson University Math Sciences
  106.         mjs@clemson.edu
  107.